//
//  TodeayHotPushVC.m
//  EveryDayNews
//
//  Created by 徐继垚 on 14/11/29.
//  Copyright (c) 2014年 徐继垚. All rights reserved.
//

#import "TodeayHotPushVC.h"

@interface TodeayHotPushVC ()<UITableViewDataSource ,UITableViewDelegate,UIWebViewDelegate>
@property (nonatomic ,retain)UILabel * contentLabel;
@property (nonatomic ,retain)UILabel * imageLabel;
@property (nonatomic ,retain)UIWebView * webView;
@end

@implementation TodeayHotPushVC
- (void)getData:(__unused id)sender
{
    self.dataSource = [NSMutableArray array];
    [TodayContentModel getDataWithBlock:^(NSMutableArray *dataSource) {
        self.dataSource = dataSource;
        [self.tableView reloadData];
    self.contentLabel.text = [_dataSource[0] title];
    self.imageLabel.text = [_dataSource[0]image_source];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSURL * url = [NSURL URLWithString:[_dataSource[0]aimage]] ;
            NSData * data = [NSData dataWithContentsOfURL:url];
            UIImage * image = [UIImage imageWithData:data];
            if (image) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    _imageView.image = image;
                 
                });
            }
        });
    } myid:_myid];
}
- (void)viewDidLoad {
    [super viewDidLoad];
  
  [self getData:nil];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, 320 * Width, 528 * Height)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.contentInset = UIEdgeInsetsMake(200, 0, 0, 0);
    [self.view addSubview:_tableView];
    
    self.imageView = [[CPKenburnsView alloc]initWithFrame:CGRectMake(0, -200 * Height, 320 * Width, 200 * Height)];
 

    [self.tableView addSubview:_imageView];
    
    UIImageView * imageview3 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 64)];
    imageview3.image = [UIImage imageNamed:@"layertop.png"];
    imageview3.alpha = 1;
    [self.view addSubview:imageview3];
    
    self.contentLabel = [[UILabel alloc]initWithFrame:CGRectMake(10 * Width, 110 * Height,  300 * Width, 80 * Height)];
    self.contentLabel.textColor = [UIColor whiteColor];
    self.contentLabel.numberOfLines = 0;
    self.contentLabel.font = [UIFont systemFontOfSize:20];
    [self.imageView addSubview:_contentLabel];
    
    self.imageLabel = [[UILabel alloc]initWithFrame:CGRectMake(10 * Width,180 * Height, 300 * Width, 10 * Height)] ;
    self.imageLabel.textColor = [UIColor lightGrayColor];
    self.imageLabel.font = [UIFont systemFontOfSize:12];

    self.imageLabel.textAlignment = NSTextAlignmentRight;
    [self.imageView addSubview:_imageLabel];
    
    UIButton * backbutton = [[UIButton alloc]initWithFrame:CGRectMake(0, 528 * Height, 320 * Width, 40 * Height)];
    [backbutton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    backbutton.backgroundColor = [UIColor redColor];
    [self.view addSubview:backbutton];
    
    self.mbview = [[MBProgressHUD alloc] initWithView:self.view];
    self.mbview.labelText = @"数据较多可能加载缓慢,请耐心等待..";
    [self.mbview setMode:MBProgressHUDModeIndeterminate];
    [self.mbview show:YES];
    [self.view addSubview:self.mbview];
    // Do any additional setup after loading the view.
}
-(void)backAction
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)webViewDidFinishLoad:(UIWebView *)webView
{
      
    NSString *str = @"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= '100%'";
    [_webView stringByEvaluatingJavaScriptFromString:str];

    
    [webView stringByEvaluatingJavaScriptFromString:
     @"var script = document.createElement('script');"
     "script.type = 'text/javascript';"
     "script.text = \"function ResizeImages() { "
     "var myimg,oldwidth;"
     "var maxwidth=300;" //缩放系数
     "for(i=0;i <document.images.length;i++){"
     "myimg = document.images[i];"
     "if(myimg.width > maxwidth){"
     "myimg.style.width = 300;"
     "myimg.style.height = 300 *myimg.height/myimg.width;"
     "}"
     "}"
     "}\";"
     "document.getElementsByTagName('head')[0].appendChild(script);"];
    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
    webView.frame = CGRectMake(0, Height * 0, Width * 320, webView.scrollView.contentSize.height + 100);
    [self.tableView reloadData];
    
    [_mbview removeFromSuperview];

    
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat content = scrollView.contentOffset.y;
    if (content < -200) {
        CGRect newFrame = self.imageView.frame;
        newFrame.origin.y =  content;
        newFrame.size.height = -content;
        self.imageView.frame = newFrame;
  
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_dataSource.count == 0) {
        return 0;
    }
    return 1;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * identifier = @"cell";
    UITableViewCell * cell  = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
    
        _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320 *Width , 568 * Height)];
        [_webView loadHTMLString:[_dataSource[0]body] baseURL:[NSURL fileURLWithPath:@"root.css"]];
         //[NSURL URLWithString:[_dataSource[0]css]]];
        
        _webView.scrollView.scrollEnabled = NO;
        self.webView.delegate = self;
        [cell.contentView addSubview:_webView];
        cell.selectionStyle = NO;
    }
       return cell;
}
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {

    //判断是否是单击
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {
        NSURL *url = [request URL];
        NSString *curUrl= [url absoluteString];
        [webView stopLoading];
        TOWebViewController * webViewController = [[TOWebViewController alloc]initWithURLString:curUrl];
 [self presentViewController:[[UINavigationController alloc] initWithRootViewController:webViewController] animated:YES completion:nil];
    webViewController.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:25/255.0 green:144/255.0 blue:211/255.0 alpha:1];
    } 
    return YES;   
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  _webView.scrollView.contentSize.height + 50;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
